package com.example.demos.web.controller;

import com.example.demos.dto.RoleDTO;
import com.example.demos.entity.Role;
import com.example.demos.service.RoleSetService;
import com.example.demos.util.CheckQueryParams;
import com.example.demos.util.PageBean;
import com.example.demos.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/v2/role")
public class RoleSetController {
    @Autowired
    private RoleSetService roleSetService;

    @GetMapping("/listByPage")
    Result<PageBean<List<Role>>> getRoleList(RoleDTO roleDTO){
        return Result.ok(roleSetService.getRoleList(roleDTO));
    }
    @PostMapping("/add")
    Result<Void> addRole(Role role){
            if(role.getName()==null){
                return Result.bizErr("您输入的字段有空的属性，请重新输入。");
            }
       if(roleSetService.addRole(role)){
           return Result.ok("新增成功");
       }
        return Result.bizErr("新增角色重复，新增失败。");
    }
    @DeleteMapping()
    Result<Void> deleteRole(BigInteger id){
        roleSetService.deleteRole(id);
        return Result.ok("删除成功");
    }
    @GetMapping("/selectone")
    Result<Role> selectRoleById(BigInteger id){
        return Result.ok(roleSetService.selectRoleById(id));
    }
    @PostMapping("/update")
    Result<Void> updateRole(Role role){
        roleSetService.updateRole(role);
    return Result.ok("修改成功");
    }
}
